package wat.serwerapp.usermgmt;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.AuthenticationException;
import javax.naming.NamingException;

/**
 *
 * @author P.B. & L.G. & K.K.
 */
public abstract class UserManager {

   private static UserManager instance = null;

   private static UserManager getInstance() {
      if (instance == null) {
         // TODO wybor wlasciwego zarzadcy uzytkownikami
         //instance = new wat.serwerapp.usermgmt.ldap.UserManager();
         instance = new UserManagerTest();
      }
      return instance;
   }

   public static final User getUser(String login, String password) throws NamingException {
      return getInstance().getUserPom(login, password);
   }

   protected abstract User getUserPom(String login, String password) throws NamingException;

   public static final Set<User> getAllUsers() throws NamingException {
      return getInstance().getAllUsersPom();
   }

   protected abstract Set<User> getAllUsersPom() throws NamingException;

   public static final void addUser(String userName, String userFullName, String password) {
      getInstance().addUserPom(userName, userFullName, password);
   }

   protected abstract void addUserPom(String userName, String userFullName, String password);

   public static void removeUser(String userName) {
      getInstance().removeUserPom(userName);
   }

   protected abstract void removeUserPom(String userName);

   public abstract class User {

      public abstract Map<String, String> getAllRights() throws NamingException;

      public abstract Set<String> getRoles() throws NamingException;

      public abstract void setRoles(Set<String> rights) throws NamingException;

      public abstract void addRole(String roleName) throws NamingException;

      public abstract void removeRole(String roleName) throws NamingException;

      public abstract String getFullName() throws NamingException;

      public abstract String getName();
   }
}


// klasa na potrzeby testow
class UserManagerTest extends UserManager {

   private Map<String, User> buffer = new HashMap<String, User>();

   protected User getUserPom(String login, String password) throws NamingException {
      User user = buffer.get(login);
      if (user == null) {
         user = new User(login, password);
         buffer.put(login, user);
      }
      return user;
   }

   protected Set<UserManager.User> getAllUsersPom() throws NamingException {
      System.out.println("getAllUsers method...");
      HashSet<UserManager.User> users = new HashSet<UserManager.User>();
      User user = new User("tarni");
      users.add(user);

      return users;
   }

   protected void addUserPom(String userName, String userFullName, String password) {

   }

   protected void removeUserPom(String userName) {
   }

   class User extends UserManager.User {

      static final String USER_SUFIX = ",ou=users,dc=wat,dc=edu,dc=pl";
      private String cn = "tarni";
      private Set<String> roles;
      private String fullName = "Tomasz Tarnawski";

      User(String login, String password) throws NamingException {
         if (!login.equals("tarni") || !password.equals("1234")) {
            throw new AuthenticationException();
         }
      }

      User(String cn) throws NamingException {
         if (!cn.equals(this.cn)) {
            throw new NamingException();
         }
      }

      public Map<String, String> getAllRights() throws NamingException {
         Map<String, String> localRights = new HashMap<String, String>();
         //localRights.put("admin", "pl.edu.wat.erp.client.administration.AddPluginClient");
         localRights.put("test", "pl.edu.wat.erp.client.testmodule.TestModulePanel");
         localRights.put("magazyn", "magazynapp.forms.MagazynMainPanel");
         //localRights.put("admin", "pl.edu.wat.erp.client.administration.UserAdministrationClient");
         return localRights;
      }

      public Set<String> getRoles() throws NamingException {
         if (roles == null) {
            roles = new HashSet<String>();
            roles.add("admin");
         }
         return roles;
      }

      public void setRoles(Set<String> rights) throws NamingException {
      }

      public void addRole(String roleName) throws NamingException {
         if (getRoles().contains(roleName)) {
            System.out.println("Uzytkownik posiada juz te role: " + roleName);
         } else {
            roles.add(roleName);
         }
      }

      public void removeRole(String roleName) throws NamingException {

         if (!getRoles().contains(roleName)) {
            System.out.println("Uzytkownik nie posiada takiej roli: " + roleName);
         } else {
            roles.remove(roleName);
         }
      }

      public String getFullName() throws NamingException {
         return fullName;
      }

      public String getName() {
         return fullName;
      }
   }
}
